package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.mapper.DtsGoodsAttributeMapper;
import com.qf.pojo.DtsGoodsAttribute;
import com.qf.pojo.DtsGoodsSpecification;
import com.qf.service.DtsGoodsAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/24 14:35
 */
@Service
public class DtsGoodsAttributeServiceImpl  implements DtsGoodsAttributeService {
    @Autowired
    private DtsGoodsAttributeMapper dtsGoodsAttributeMapper;
    @Override
    public List<DtsGoodsAttribute> selectByGoodsId(Integer id) {
        return  dtsGoodsAttributeMapper.selectList(new QueryWrapper<DtsGoodsAttribute>().eq("goods_id",id).eq("deleted",false));
    }

    @Override
    public void update(List<DtsGoodsAttribute> dtsGoodsAttributes,  Integer goodsId) {
        ArrayList<DtsGoodsAttribute> insertDtsGoodsAttribute= new ArrayList<>();
        ArrayList<DtsGoodsAttribute> deleteDtsGoodsAttributes = new ArrayList<>();
        List<DtsGoodsAttribute> goodsAttributes = dtsGoodsAttributeMapper.selectList(new QueryWrapper<DtsGoodsAttribute>().eq("goods_id", goodsId).eq("deleted",false));
        for (DtsGoodsAttribute goodsAttribute : goodsAttributes) {
            if(!dtsGoodsAttributes.contains(goodsAttribute)){
                deleteDtsGoodsAttributes.add(goodsAttribute);
            }
        }
        for (DtsGoodsAttribute dtsAttribute : dtsGoodsAttributes) {
            if(!goodsAttributes.contains(dtsAttribute)){
                insertDtsGoodsAttribute.add(dtsAttribute);
            }
        }
        dtsGoodsAttributeMapper.insertList(insertDtsGoodsAttribute);
        dtsGoodsAttributeMapper.updateList(deleteDtsGoodsAttributes);
    }

    @Override
    public void insert(List<DtsGoodsAttribute> attributes) {
          dtsGoodsAttributeMapper.insertList((ArrayList<DtsGoodsAttribute>) attributes);
    }
}
